package com.itheima.question.timer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.safe.AliyunSafeTemplate;
import com.itheima.question.common.contants.RedisKeyContstants;
import com.itheima.question.mapper.QuestionItemMapper;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.pojo.Question;
import com.itheima.question.pojo.QuestionItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @ClassName Audittask
 * @Description TODO
 * @Author lh
 * @Date 2021/12/22
 **/
@Component
public class AuditTask {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionItemMapper questionItemMapper;

    @Autowired
    private AliyunSafeTemplate aliyunSafeTemplate;

    @Scheduled(cron = "0 0 2 * * ?")
    public void auditQuestion() {
        //获取redis内容,获取待审核问题id
        Set<String> questionIds = redisTemplate.opsForSet().members(RedisKeyContstants.AUDIT_QUESTION);
        if(questionIds!=null && questionIds.size()>0) {
            for (String questionId:questionIds) {
                audit(questionId);
            }
        }


        //遍历集合到
    }

    private void audit(String questionId) {
        StringBuilder sb = new StringBuilder();

        //查询问题
        Question question = questionMapper.selectById(questionId);
        if(question == null) {
            return;
        }
        sb.append(question.getSubject());
        //查询问题选项
        QueryWrapper<QuestionItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",questionId);
        List<QuestionItem> questionItems = questionItemMapper.selectList(queryWrapper);
        if(questionItems!=null && questionItems.size()>0) {
            for (QuestionItem questionItem: questionItems) {
                sb.append(questionItem.getContent());
            }
        }
        try {
            aliyunSafeTemplate.greenTextScan(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
